package yns.springboot.guide.demo.controller.data.rabbitmq;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import tools.jackson.databind.ObjectMapper;
import yns.springboot.guide.demo.configure.data.rabbitmq.RabbitmqConfigure;

@RestController
@RequestMapping("/rabbitmq/template")
public class TestRabbitmqTemplateController {
	
	// http://localhost:8080/rabbitmq/template
	// http://localhost:8080/rabbitmq/template/producer
	// http://localhost:8080/rabbitmq/template/producer2
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Autowired
	private ObjectMapper objectMapper;
	
	@Autowired
	private RabbitmqConfigure rabbitmqConfigure;
	

	@GetMapping()
	public String testRabbitmq() {

		HashMap<String, String> hashMap = new HashMap<>();
		hashMap.put("key", "test");
		hashMap.put("key2", "test2");
		hashMap.put("key3", LocalDateTime.now().toString());

		String json = objectMapper.writeValueAsString(hashMap);
		logger.info(json);

		String queue = rabbitmqConfigure.getQueue();
		String exchange = rabbitmqConfigure.getExchange();
		String routingKey = rabbitmqConfigure.getRoutingKey();
		
		
//		rabbitTemplate.convertAndSend("test", json);
//		rabbitTemplate.convertAndSend("exchange.direct","test", json);
//		rabbitTemplate.convertAndSend("test2", LocalDateTime.now().toString());
		
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.setPriority(1);
		messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
		Message message = new Message(json.getBytes(), messageProperties);
		
		logger.info("testRabbitmq queue : " + queue);
		logger.info("testRabbitmq exchange : " + exchange);
		logger.info("testRabbitmq routingKey : " + routingKey);
		logger.info("testRabbitmq messageProperties : " + messageProperties.toString());
		logger.info("testRabbitmq message : " + message.toString());
		
		rabbitTemplate.send(exchange,"",message);
//		rabbitTemplate.send(exchange,routingKey,message);

		return "test.rabbitmq.template--testRabbitmq";
	}
	
	@GetMapping("/producer")
	public String producerRabbitmq() throws IOException {
		
		HashMap<String, String> hashMap = new HashMap<>();
		hashMap.put("key", "test");
		hashMap.put("key2", "test2");
		hashMap.put("key3", LocalDateTime.now().toString());

		String json = objectMapper.writeValueAsString(hashMap);
		logger.info(json);

//		String queue = rabbitmqConfigure.getQueue();
//		String exchange = rabbitmqConfigure.getExchange();
//		String routingKey = rabbitmqConfigure.getRoutingKey();
		
		String exchange = "exchange.direct";
		String routingKeyOne = "test";
		String routingKeyTwo = "test2";
		
		logger.info("producerRabbitmq exchange : " + exchange);
		logger.info("producerRabbitmq routingKeyOne : " + routingKeyOne);
		logger.info("producerRabbitmq routingKeyTwo : " + routingKeyTwo);
		
		rabbitTemplate.convertAndSend(routingKeyOne, json);
		rabbitTemplate.convertAndSend(exchange,routingKeyOne, json);
		rabbitTemplate.convertAndSend(routingKeyTwo, LocalDateTime.now().toString());

		return "test.rabbitmq.template--producer";
	}
	
	@GetMapping("/producer2")
	public String producer2Rabbitmq() throws IOException {
		
		HashMap<String, String> hashMap = new HashMap<>();
		hashMap.put("key", "test");
		hashMap.put("key2", "test2");
		hashMap.put("key3", LocalDateTime.now().toString());

		String json = objectMapper.writeValueAsString(hashMap);
		logger.info(json);

		String queue = rabbitmqConfigure.getQueue();
		String exchange = rabbitmqConfigure.getExchange();
		String routingKey = rabbitmqConfigure.getRoutingKey();
		
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.setPriority(1);
		messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
		Message message = new Message(json.getBytes(), messageProperties);
		
		logger.info("producer2Rabbitmq queue : " + queue);
		logger.info("producer2Rabbitmq exchange : " + exchange);
		logger.info("producer2Rabbitmq routingKey : " + routingKey);
		
		logger.info("producer2Rabbitmq messageProperties : " + messageProperties.toString());
		logger.info("producer2Rabbitmq message : " + message.toString());
		
		rabbitTemplate.send(exchange,"",message);
		rabbitTemplate.send(exchange,routingKey,message);

		return "test.rabbitmq.template--producer2";
	}

}
